package com.lge.pitchingcoins.ui.clients.playgame;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import com.lge.pitchingcoins.R;
import com.lge.pitchingcoins.util.PitchingCoinsLog;

public class GamePlayBasedView extends SurfaceView implements SurfaceHolder.Callback, Runnable {
    private SurfaceHolder holder = null;

    private Bitmap bitmap = null;
    private Paint mPaint = null;
    private Point mWindowPoint = null;

    private Thread thread = null;
    private Canvas canvas = null;
    private int moveSpeed = 0;

    
    // Constructors
    public GamePlayBasedView(Context context) {
        super(context);
        PitchingCoinsLog.d("GamePlayBasedView");

        holder = getHolder();
        holder.addCallback(this);
        holder.setFixedSize(getWidth(), getHeight()); // size jijung
        setFocusable(true);

        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setTextSize(20);
        mPaint.setColor(Color.BLACK);
        mPaint.setTypeface(Typeface.DEFAULT_BOLD);

    }

    public GamePlayBasedView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public GamePlayBasedView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    
    // Setters and Getters
    public Thread getThread() {
        return thread;
    }

    public void setPosition(int x, int y) {

    }

    public void setMoveSpeed(int moveSpeed) {
        this.moveSpeed = moveSpeed;
    }

    
    // Callback Methods
    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        PitchingCoinsLog.d("surfaceCreated");
        mWindowPoint = new Point();
        mWindowPoint.x = getWidth();
        mWindowPoint.y = getHeight();

        Bitmap tempBmp = BitmapFactory.decodeResource(getResources(),
                R.drawable.coin);

        bitmap = Bitmap.createScaledBitmap(tempBmp, 200, 200, true);

        thread = new Thread(this);
        thread.start();
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        if (thread != null) {
            thread.interrupt();
        }
    }

    @Override
    public void run() {
        PitchingCoinsLog.d("run");

        while (!(Thread.currentThread().isInterrupted())) {
            //        while (true) {

            canvas = holder.lockCanvas(null);
            try {

                //                if (canvas != null) {
                synchronized (holder) {
                    doGameViewDrawing(canvas);
                    Thread.sleep(40);
                }
                //                }

            } catch (InterruptedException e) {
                PitchingCoinsLog.d("surfaceview exception");
                // Thread.currentThread().stop();
                Thread.currentThread().interrupt();
                e.printStackTrace();
            } finally {
                if (canvas != null) {
                    holder.unlockCanvasAndPost(canvas);
                }
                //                try {
                //                    holder.unlockCanvasAndPost(canvas);
                //                } catch (Exception e) {
                //                    PitchingCoinsLog.d("unlock exception");
                //                    e.printStackTrace();
                //                }
            }
        }
    }

    
    // Member Methods
    private void doGameViewDrawing(Canvas canvas) {
        mWindowPoint.y -= moveSpeed;
        if (mWindowPoint.y < -bitmap.getWidth()) {
            mWindowPoint.y = getHeight();
            moveSpeed = 0;
            //			Thread.currentThread().stop();
            Thread.currentThread().interrupt();
            PitchingCoinsLog.d("doGameViewDrawing");
        }

        if (canvas != null) {
            canvas.drawColor(Color.LTGRAY);
            canvas.drawBitmap(bitmap, (mWindowPoint.x / 2) - (bitmap.getWidth() / 2),
                    mWindowPoint.y
                            - (bitmap.getHeight() + 50), null);
        }
    }
}